package com.zkx.blogweb.controller;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkx.blogweb.entity.CodeMsg;
import com.zkx.blogweb.entity.IdArticle;
import com.zkx.blogweb.entity.Message;
import com.zkx.blogweb.entity.User;
import com.zkx.blogweb.mapper.service.BlogServiceImp;
import com.zkx.blogweb.mapper.service.IdArticleServiceImp;
import com.zkx.blogweb.mapper.service.UserMapperImp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author zkx
 */
@Slf4j
@Controller
public class LoginController {

    @Autowired
    UserMapperImp userMapperImp;

    @Autowired
    BlogServiceImp blogServiceImp;

    @Autowired
    IdArticleServiceImp idArticleServiceImp;

    @RequestMapping(value = {"/index","/"},method = {RequestMethod.POST,RequestMethod.GET})
    public String index(){
        return "index";
    }

    @PostMapping("/main")
    @ResponseBody
    public Message main(HttpSession session, User user){
        Integer userBlogId = user.getUserBlog();
        User user1 = userMapperImp.getUser(userBlogId);
        String userPassword = user.getUserPassword();
        if(user1!=null && userPassword.equals(user1.getUserPassword())){
            session.setAttribute("user",user1);
            return Message.success(user1,"hello");
        }
        return Message.error(CodeMsg.REQUEST_ERROR,"/");
    }

    @GetMapping("/hello")
    public String hello(HttpSession session, Model model){
        
        //获取所有员工以及，所有的博文
        int blogerNums = userMapperImp.blogerNums();
        model.addAttribute("blogerNums",blogerNums);
        //博文数量
        int allArticles = idArticleServiceImp.allArticles();
        model.addAttribute("allArticles",allArticles);
        //User user = (User)session.getAttribute("user");
        return "hello";
    }



    /**
     *
     * @return
     *
     * */
    @RequestMapping(value = "/blogname",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String getPageName(){

        HashMap<String, String> allBlogUserName = idArticleServiceImp.getAllBlogUserName();
        log.info("成功");
        return JSON.toJSONString(allBlogUserName);
    }

    /**
     * 前端发送ajax请求，刷新页面
     * @param 
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/page/{id}",method = {RequestMethod.POST,RequestMethod.GET})
    public PageInfo<IdArticle> getPage(@PathVariable("id") Integer id, Model model){
        PageInfo<IdArticle> pageInfo = idArticleServiceImp.getPageInfo(id, 10);
        return pageInfo;
    }


    @RequestMapping(value = "/changeBlog",method = {RequestMethod.POST,RequestMethod.GET})
    public String changeBlog(HttpSession session){
        System.out.println((User)session.getAttribute("user"));
        session.removeAttribute("user");
        return "redirect:/index";
    }


    @Transactional
    @ResponseBody
    @RequestMapping(value = "/logout",method = {RequestMethod.POST,RequestMethod.GET})
    public String logout(Integer userId,HttpSession session){
        System.out.println(userId);
        User user = userMapperImp.selectByUserId(userId);
        Integer userBlog = user.getUserBlog();
        System.out.println(userBlog);

        try{
            //删除博客号的所有文章
            idArticleServiceImp.deleteBlogByUserId(userId);

            //再删除
            userMapperImp.deleteById(userId);

            //还要删除博客号
            blogServiceImp.deleteByUserId(userBlog);

            //更新session
            session.removeAttribute("user");

            return "注销成功";
        }catch (Exception e){
            throw new RuntimeException("删除错误");
        }

    }


    /**
     * 排行榜
     * @return
     */
    @Transactional
    @ResponseBody
    @RequestMapping(value = "/rank",method = {RequestMethod.POST,RequestMethod.GET})
    public List<IdArticle> rank(){
        List<IdArticle> rankBlogers = new ArrayList<>();
        rankBlogers = idArticleServiceImp.getRankTop();
        return rankBlogers;
    }
}
